package com.powerunion.datacollection.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.ApproveTargetsDataDao;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.model.ActivityInfo;
import com.powerunion.datacollection.model.ApproveTargetsData;
import com.powerunion.datacollection.model.jsonObject.GlobalTargetsDataJSONObject;
import com.powerunion.datacollection.model.jsonObject.JSONDataBean;
import com.powerunion.datacollection.model.query.ApproveTargetsDataParams;
import com.powerunion.util.log.LogHelper;

public class ApproveTargetsDataManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			ApproveTargetsDataManager.class);
	private ApproveTargetsDataDao dao = null;

	public ApproveTargetsDataManager() {
		super();
		this.dao = (ApproveTargetsDataDao) DaoFactory.getInstance().getDao(
				ApproveTargetsDataDao.class);
	}

	public boolean save(String saveStr,ApproveTargetsDataParams params)
	 throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (params == null) {
			String[] args = { "params" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter params NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		
		SubmissionWorkItemManager swm = new SubmissionWorkItemManager();
		ActivityInfo activityInfo = new ActivityInfo();
		activityInfo.setActivityId(params.getAwId());
		activityInfo.setProcId(params.getProcId());
		activityInfo.setFillTypeOrapproveType(8);
		activityInfo.setActivityName("审批步骤报送任务");
		
		boolean flag = false;
		flag =  swm.initSubWork(params.getUpUser(), activityInfo, params.getFormId());
		
		if(flag){
			Map gMap = new HashMap();
			gMap.put("data", GlobalTargetsDataJSONObject.class);
			gMap.put("dataArray", ApproveTargetsData.class);
			JSONDataBean am = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveStr), JSONDataBean.class,gMap);
			List list = am.getData();
			
			if(!list.isEmpty()){
				flag = this.dao.save(list, params);
			}
		}

		
		return flag;
	}	
	
	public List getTargetSubmiData(String documentId,String procId,String actId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(documentId)) {
			String[] args = { "documentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter documentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(actId)) {
			String[] args = { "actId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter actId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getTargetSubmiData(documentId, procId, actId);
	}
	
}
